package com.ruoyi.web.controller.api;

import com.ruoyi.common.constant.RedisConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.paas.domain.XspUserSiginLog;
import com.ruoyi.paas.domain.XspUserSign;
import com.ruoyi.paas.service.IXspUserSiginLogService;
import com.ruoyi.paas.service.IXspUserSignService;
import com.ruoyi.redis.RedisCacheUtil;
import com.ruoyi.xsp.domain.XspUser;
import com.ruoyi.xsp.service.IXspUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 任务中心 包含签到 api
 *
 * @author zhiminhu
 * @date 2021-01-22
 */
@Api(value = "任务中心", tags = "任务中心")
@RestController
@RequestMapping("/api/task")
public class XspTaskApiController extends BaseApiController {
    private static final Logger log = LoggerFactory.getLogger(XspTaskApiController.class);
    @Autowired
    private IXspUserSignService xspUserSignService;
    @Autowired
    private IXspUserSiginLogService xspUserSiginLogService;

    /**
     * 每日签到
     */
    @ApiOperation("每日签到")
    @GetMapping("/sign")
    @ResponseBody
    public AjaxResult sign(HttpServletRequest request) {
        AjaxResult result = checkToken(request);
        if (result.get(AjaxResult.CODE_TAG).equals(AjaxResult.Type.SUCCESS.value())) {
            XspUser user = (XspUser) result.get(AjaxResult.DATA_TAG);
            XspUserSign sign = xspUserSignService.selectXspUserSignByUserCode(user.getUserCode());
            if (sign == null) {
                //第一次签到 先插入
                if (insertSign(user.getUserCode(),user) <= 0) {
                    return AjaxResult.error("签到失败");
                }
            } else {
                sign.setNickname(user.getNickname());
                XspUserSiginLog todayLog=xspUserSiginLogService.selectXspUserSiginLogForTodayByUserCode(user.getUserCode());
                if(todayLog !=null){
                    return AjaxResult.error("已经签到");
                }else {
                    XspUserSiginLog log = xspUserSiginLogService.selectXspUserSiginLogForYesterdayByUserCode(user.getUserCode());
                    //总数加1
                    sign.setSignCount(sign.getSignCount() + 1);
                    if (log == null) {
                        //昨天没有签到 重新签到
                        sign.setContinueSign(1);
                    } else {
                        //昨天签到了，今天叠加
                        sign.setContinueSign(sign.getContinueSign()+1);
                    }
                    if (xspUserSignService.updateXspUserSign(sign) <= 0) {
                        return AjaxResult.error("签到失败");
                    }
                }
            }
        } else {
            return result;
        }
        return AjaxResult.success();
    }

    private int insertSign(Long userCode,XspUser user ) {
        XspUserSign sign = new XspUserSign();
        sign = new XspUserSign();
        sign.setUserCode(userCode);
        sign.setNickname(user.getNickname());
        sign.setSignCount(1);
        sign.setContinueSign(1);
        sign.setCreateTime(new Date());
        sign.setUpdateTime(new Date());
        return xspUserSignService.insertXspUserSign(sign);
    }

    /**
     * 获取我的签到信息
     */
    @ApiOperation("获取我的签到信息")
    @GetMapping("/getSign")
    @ResponseBody
    public AjaxResult getSignByUserCode(HttpServletRequest request) {
        AjaxResult result = checkToken(request);
        if (result.get(AjaxResult.CODE_TAG).equals(AjaxResult.Type.SUCCESS.value())) {
            XspUser user = (XspUser) result.get(AjaxResult.DATA_TAG);
            XspUserSign sign=xspUserSignService.selectXspUserSignByUserCode(user.getUserCode());
            if(DateUtils.isSameDay(new Date(),sign.getUpdateTime())){
                //已签到
                sign.setSignStatus(true);
            }else {
                sign.setSignStatus(false);
            }
            return AjaxResult.success(sign);
        } else {
            return result;
        }
    }


    /**
     * 获取任务列表中心
     */
    @ApiOperation("获取任务列表中心")
    @GetMapping("/getList")
    @ResponseBody
    public AjaxResult getList(HttpServletRequest request) {
        AjaxResult result = checkToken(request);
        if (result.get(AjaxResult.CODE_TAG).equals(AjaxResult.Type.SUCCESS.value())) {
            XspUser user = (XspUser) result.get(AjaxResult.DATA_TAG);

            return AjaxResult.success();
        } else {
            return result;
        }
    }

}
